package com.tjjingpan.wifidirectdemo.broadcast

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.wifi.WifiManager
import android.net.wifi.p2p.WifiP2pDevice
import android.net.wifi.p2p.WifiP2pDeviceList
import android.net.wifi.p2p.WifiP2pManager
import android.os.Build
import android.util.Log
import com.tjjingpan.wifidirectdemo.utils.WifiDirectManager

class WifiDirectReceiver(val wifiP2pManager: WifiP2pManager,
                         val channel:WifiP2pManager.Channel,
                         val directReceiver: WifiDirectReceiver
    ) :BroadcastReceiver(){
    val TAG = "WifiDirectReceiver"
    /**
     * 写一个便捷的注册方法，动态注册的时候就不用写initerFilter了
     */
    fun registerReceiver(context: Context){
        val intentFilter = IntentFilter()
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION)
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION)
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION)
        intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION)
        context.registerReceiver(this,intentFilter)
    }
    override fun onReceive(context: Context?, intent: Intent?) {
        when(intent?.action){
            /**
             * 当wifi功能打开或关闭时候系统会发送WIFI_P2P_STATE_CHANGED_ACTION广播
             * 并不是指是否已成功连上wifi
             */
            WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION ->{
                    //get the state of current device
                val state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE,
                WifiP2pManager.WIFI_P2P_STATE_DISABLED)
                if(state == WifiP2pManager.WIFI_P2P_STATE_ENABLED){

                    //wifi p2p enable
                    Log.d(TAG,"wifi enable")
                }
                else{
                    //wifi p2p disEnable
                   Log.d(TAG,"wifi disable")
                }
            }
            /**
             * 当前设备的详细信息发生变化的时候，系统会发送WIFI_P2P_THIS_DEVICE_CHANGED_ACTION广播
             */
            WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION ->{
                val device : WifiP2pDevice? = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE)
                Log.d(TAG,device.toString())
            }
            /**
             * 当可连接的对等节点列表发生改变的时候，会发送该广播
             * invoke when the list of peers find,register,lost
             */
            WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION ->{
                    //api > 18 have this extra info,
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
                    //Log.d(TAG,"WIFI_P2P_PEERS_CHANGED_ACTION")
                    //val wifiP2pList: WifiP2pDeviceList? = intent.getParcelableExtra(WifiP2pManager.EXTRA_P2P_DEVICE_LIST)
                   //Log.d(TAG,wifiP2pList.toString())

                }else{ //if ths sdk version lower than 18
                    //get wifiP2pDeviceList by call WifiP2pManager.requestPeers to get
                    WifiDirectManager.requestPeers()
                }
            }
            /**
             * 当一个连接建立或断开的时候，系统会发送该广播
             * This action received when connection setup or dis-setup
             */
            WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION ->{

            }
        }
    }
}